/*
 *   Copyright 2008 Luca Bellonda <lbellonda@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#ifndef SCANTHREAD_H
#define SCANTHREAD_H

#include <QMutex>
#include <QThread>
#include <QDBusConnection>
#include <QtXml/QtXml>
#include <KConfig>
#include <KConfigGroup>
#include <dbusappletItem.h>

#define SOURCE_SYSTEM_SERVICES "SystemServices"
#define SOURCE_SESSION_SERVICES "SessionServices"

class ScanThread : public QThread
{
    Q_OBJECT

public:
    ScanThread(QObject *parent = 0);
    ~ScanThread();

    void scan();
    void getData(QVariant &system, QVariant &session);

signals:
    void scanEnd();

protected:
    void run();

private:

    KConfig *m_config;
    bool m_isAbort;
    QMutex m_mutex;
    DBusAppletItem *m_resultSession;
    DBusAppletItem *m_resultSystem;

    void execute(DBusAppletItem *resultSession, DBusAppletItem *resultSystem);
    void setDBusData(const QString& name, const QStringList &services, QDBusConnection &connection, DBusAppletItem *result);
    void introspect(const QString &service, const QString &path, QDBusConnection &connection, int &idObject, const int idParent, DBusAppletItem *result);
    void scan(const QString &service, QDomNode &doc, QDBusConnection &connection, int &idObject, const int idParent, DBusAppletItem *result);
    void scanForInterfaces(const QString &service, QDomNode &node);
    void scanForMethods(QDomNode &node, int &idObject, const int idParent, DBusAppletItem *result);
    QString scanForArguments(const QString &name, QDomNode &node);
    void addToResult(QStringList &result, const int id, const int idParent, const QString &type, const QString &value);
    static void copyStringList(const QStringList &inp, QStringList &outp);
};


#endif